<?php

/**
 * task actions.
 *
 * @package    lbreminder
 * @subpackage task
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class taskActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{

	}

	public function executeSelectFromMap(sfWebRequest $request) {

	}

	public function executeVoteForRaw(sfWebRequest $request) {
	
	}

	public function executeSelectFromPoi(sfWebRequest $request) {

	}

	public function executeApkDownload(sfWebRequest $request) {
		$this->redirect('/downloads/raw.apk');
	}

	public function executeAboutUs(sfWebRequest $request) {

	}

	public function executeWhatsRaw(sfWebRequest $request)
	{

	}

	public function executeList(sfWebRequest $request) {
		$user_id = $this->getUser()->getGuardUser()->getId();
		$this->tasks = TaskPeer::getTasksByUserId($user_id);
	}

	public function executeNew(sfWebRequest $request)
	{
		$task = new Task();
		if($request->hasParameter('selected_lat') && $request->getParameter('selected_lat') != '-1') {
			$task_location = new TaskLocation();
			$task_location->setLatitude($request->getParameter('selected_lat'));
			$task_location->setLongitude($request->getParameter('selected_lng'));
			$this->getUser()->setAttribute('task_location', $task_location);
		}
		$this->task = $task;
		$this->task_types = TaskTypePeer::doSelect(new Criteria());
		$this->task_priorities = TaskPriorityPeer::doSelect(new Criteria());
		//		$this->form = new TaskForm($task);
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->form = new TaskForm();
		$this->processForm($request);
		$this->setTemplate('new');
	}

	public function executeUpdate(sfWebRequest $request) {
		$this->myId = $request->getParameter('id');
	}
	protected function processForm(sfWebRequest $request)
	{
		//		$values = $request->getParameter($form->getName());
		$task = new Task();
		$user_id = $this->getUser()->getGuardUser()->getId();
		$task->setName($request->getParameter('name'));
		$task->setDescription($request->getParameter('description'));
		$task->setUserId($user_id);
		$task->setTypeId($request->getParameter('type_id'));
		$task->setPriorityId($request->getParameter('priority_id'));
		$task->setLocationId(1);
		$arrDateTime = $this->findDateTime($request->getParameter('duedatetime'));
		$task->setDay((int)$arrDateTime['day']);
		$task->setMonth((int)$arrDateTime['month']);
		$task->setYear((int)$arrDateTime['year']);
		$task->setHour((int)$arrDateTime['hour']);
		$task->setMinute((int)$arrDateTime['minute']);
		$task->setAmpm($arrDateTime['ampm']);
		
		if($this->getUser()->hasAttribute('task_location')) {
			$location = $this->getUser()->getAttribute('task_location');
			$task->setTaskLocation($location);
		}

		try {
			$task->save();
		} catch (Exception $e) {
			echo nl2br($e->getMessage());
			return;
		}

		$this->redirect('task_list_view', $task);
	}

	private function findDateTime($strDateTime)
	{
		if(strlen($strDateTime) >= 18)
		{
			$arrDateTime = array();

			$day = substr($strDateTime,0,2);
			$month = substr($strDateTime,3,2);
			$year = substr($strDateTime,6,4);

			$hour = substr($strDateTime,11,2);
			$min = substr($strDateTime,14,2);
			$ampm = substr($strDateTime,16,2);

			$arrDateTime['day'] = $day;
			$arrDateTime['month'] = $month;
			$arrDateTime['year'] = $year;
			$arrDateTime['hour'] = $hour;
			$arrDateTime['minute'] = $min;
			$arrDateTime['ampm'] = $ampm;

			return $arrDateTime;
		}
		return null;
	}
}
